/*
 * Copyright 2001-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package code.buildit.plugin.intention;

import code.buildit.plugin.BuildItActionHandler;
import code.buildit.plugin.BuildItResourceBundle;
import code.buildit.plugin.adapter.PsiAdapter;
import code.buildit.plugin.adapter.PsiClassAdapter;
import code.buildit.plugin.processor.ElementBuilder;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class BuildItErrorIntention extends PsiElementBaseIntentionAction {
    public boolean isAvailable(@NotNull Project project, Editor editor, @Nullable PsiElement psiElement) {
        PsiElement expression = PsiTreeUtil.getParentOfType(psiElement, PsiMethodCallExpression.class);
        return expression != null;
    }

    public void invoke(@NotNull Project project, Editor editor, PsiFile psiFile) throws IncorrectOperationException {
        PsiElement currentElement = new PsiAdapter(editor.getProject()).findMatchingElement(psiFile, editor);

        if (currentElement == null) return;

        PsiElement entireExpression = PsiTreeUtil.getParentOfType(currentElement, PsiMethodCallExpression.class);
        if (entireExpression == null)
            return;

        PsiMethodCallExpression psiMethodCallExpression = (PsiMethodCallExpression) entireExpression;
        PsiExpressionList psiExpressionList = psiMethodCallExpression.getArgumentList();
        PsiExpression[] psiExpressions = psiExpressionList.getExpressions();
        PsiReferenceExpression methodExpression = psiMethodCallExpression.getMethodExpression();
        PsiExpression qualifierExpression = methodExpression.getQualifierExpression();

        PsiManager psiManager = psiFile.getManager();
        if (qualifierExpression == null)
            return;

        PsiType type = qualifierExpression.getType();
        if (type == null)
            return;

        PsiClass aClass = new PsiClassAdapter(project).findPsiClassByName(project, psiManager, type.getCanonicalText());

        String methodName = methodExpression.getReferenceName();
        PsiType variableType = psiExpressions[0].getType();

        if (variableType == null || methodName == null || aClass == null)
            return;

        PsiField psiField = new ElementBuilder().constructField(aClass, methodName, variableType);
        new BuildItActionHandler().doExecuteAction(aClass, new PsiField[]{psiField}, false);

        PsiFile containingFile = aClass.getContainingFile();
        containingFile.navigate(true);

    }


    @NotNull
    public String getText() {
        //noinspection UnresolvedPropertyKey
        return BuildItResourceBundle.message("buildit.error.name");
    }

    @NotNull
    public String getFamilyName() {
        //noinspection UnresolvedPropertyKey
        return BuildItResourceBundle.message("buildit.error.family.name");
    }


}
